import datetime
import sqlite3


class db_operator:
    def __init__(self, url):
        self.url = url

    def set_url(self, url):
        self.url = url

    def get_url(self):
        return self.url

    """判断记录是否存在"""
    def judge_data_exists(self):
        conn = sqlite3.connect("database\\api_count.db")
        cursor = conn.cursor()
        cursor.execute(
            """
            SELECT api_name FROM api_record_count WHERE api_name="{}"          
            """.format(self.url)
        )
        result = cursor.fetchall()
        cursor.close()
        conn.close()
        #判断查询结果是否为空，为空则返回false,不为空则返回true
        if result:
            return True
        else:
            return False

    """
    获取api的更新时间
    """
    def get_update_time(self):
        id_data = self.get_latest_id()
        conn = sqlite3.connect("database\\api_count.db")
        cursor = conn.cursor()
        cursor.execute(
            """
            SELECT t.record_update_time FROM api_record_count t WHERE t.id={}
            """.format(id_data)
        )
        result = cursor.fetchall()
        cursor.close()
        conn.close()
        for row in result:
            return row[0]

    """获取最新一条api的id"""
    def get_latest_id(self):
        conn = sqlite3.connect("database\\api_count.db")
        cursor = conn.cursor()
        cursor.execute(
            """
            SELECT id from api_record_count WHERE api_name="{}" ORDER BY record_update_time DESC LIMIT 1;          
            """.format(self.url)
        )
        result = cursor.fetchall()
        cursor.close()
        conn.close()
        for row in result:
            return row[0]

    """获取最新一条数据的计数count值"""

    def get_count_data(self):
        conn = sqlite3.connect("database\\api_count.db")
        cursor = conn.cursor()
        cursor.execute(
            """
            SELECT t.count FROM api_record_count t WHERE t.api_name="{}" ORDER BY t.record_update_time DESC LIMIT 1;
            """.format(self.url)
        )
        result = cursor.fetchall()
        for row in result:
            return row[0]

    """更新最新一条数据的状态为record_tense"""

    def update_latest_id_tense_finished(self):
        id_data = self.get_latest_id()
        conn = sqlite3.connect("database\\api_count.db")
        cursor = conn.cursor()
        cursor.execute(
            """
            UPDATE api_record_count  SET record_tense="{}" WHERE id={}
            """.format("finished", id_data)
        )
        conn.commit()
        cursor.close()
        conn.close()

    """
    1.在配置时间内
    2.更新最新的一条api数据，该数据是在配置时间内新增，计数+1,并同步更新创建时间
    """

    def update_latest_id_count_add_one(self):
        timer = str(datetime.datetime.now())[:19]
        count = int(self.get_count_data()) + 1
        id_data = self.get_latest_id()
        conn = sqlite3.connect("database\\api_count.db")
        cursor = conn.cursor()
        cursor.execute(
            """
            UPDATE api_record_count  SET count="{}",record_update_time="{}" WHERE id={}
            """.format(count, timer, id_data)
        )
        conn.commit()
        cursor.close()
        conn.close()

    """
       1.超过配置的时间
       2.新增一条相同的数据
       """

    def insert_api_record_count_data(self):
        timer = str(datetime.datetime.now())[:19]
        conn = sqlite3.connect("database\\api_count.db")
        cursor = conn.cursor()
        cursor.execute(
            """
    insert into api_record_count(api_name,count,record_create_time,record_update_time) values("{}","1","{}","{}");
        """
            .format(self.url, timer, timer, "recoding"))
        cursor.execute("COMMIT;")
        result = cursor.fetchall()
        cursor.close()
        conn.close()

    """插入记录表"""
    def insert_api_record_data(self):
        timer = str(datetime.datetime.now())[:19]
        conn = sqlite3.connect("database\\api_count.db")
        cursor = conn.cursor()
        cursor.execute(
            """
    insert into api_record(api_name,record_create_time,record_update_time) values("{}","{}","{}");
        """
            .format(self.url, timer, timer))
        cursor.execute("COMMIT;")
        result = cursor.fetchall()
        cursor.close()
        conn.close()

    @staticmethod
    def delete_all_data():
        conn = sqlite3.connect("database\\api_count.db")
        cursor = conn.cursor()
        cursor.execute(
            """ 
            DELETE FROM api_record_count;
            """
        )
        conn.commit()
        cursor.execute(

            """
            DELETE FROM api_record;
            """
        )
        conn.commit()
        cursor.close()
        conn.close()


if __name__ == '__main__':
    pass
